// pages/profile/index.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo: null
  },
  onChooseAvatar(ev) {
    const { avatarUrl } = ev.detail
    wx.uploadFile({
      url: wx.http.baseURL + '/upload',
      filePath: avatarUrl,
      name: 'file',
      header: {
        Authorization: `Bearer ${getApp().token}`,
      },
      formData: {
        type: 'avatar'
      },
      success: async (res) => {
        const resData = JSON.parse(res.data)
        // console.log(433, resData);
        const avatar = resData.data.url
        await wx.http({
          url: '/userInfo',
          method: 'put',
          data: { avatar }
        })
        wx.utils.toast('修改头像成功')
        this.setData({
          "userinfo.avatar": avatar
        })
      }
    })
  },
  async onNickNameChange(ev) {
    const nickName = ev.detail
    // console.log(ev);
    if (!nickName) return wx.utils.toast('昵称不能为空')
    await wx.http({
      url: '/userInfo',
      method: 'put',
      data: {
        nickName
      },
    })
    wx.utils.toast('修改成功')
    this.setData({
      "userinfo.nickName": nickName
    })
  }
})